{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "_llm = ChatOpenAI(\n",
    "    api_key=\"ollama\",\n",
    "    model=\"qwen2.5:7b\",\n",
    "    base_url=\"http://192.168.10.11:60026/v1\",\n",
    "    temperature=0.7,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "from langchain_core.tools import tool\n",
    "\n",
    "from typing import Annotated\n",
    "from langchain_core.tools import tool\n",
    "\n",
    "\n",
    "@tool\n",
    "def weather(city: Annotated[str, \"被查询的城市,用中文输入\"]) -> str:\n",
    "    \"\"\"\n",
    "    用于查询输入城市今日的天气状况。\n",
    "    \"\"\"\n",
    "    if city == \"上海\":\n",
    "        return \"上海今日有台风12级\"\n",
    "    else:\n",
    "        return \"天气晴朗,风和日丽\"\n",
    "\n",
    "\n",
    "@tool\n",
    "def fish(river: Annotated[str, \"被查询的河流,用中文输入\"]) -> str:\n",
    "    \"\"\"\n",
    "    用于查询输入河流有哪些鱼类\n",
    "    \"\"\"\n",
    "    if \"新安江\" in river:\n",
    "        return \"江里有石斑鱼、鲫鱼、草鱼、黑鱼、花鲢、青鱼\"\n",
    "    else:\n",
    "        return \"风浪越大鱼越贵\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'看来“风浪越大鱼越贵”这句话适用于艾溪江，但具体有哪些鱼类生活在这里的信息未能直接获取到。我们可以尝试其他方式来进一步查询，或者你有其他问题需要帮助吗？'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langgraph.prebuilt import create_react_agent\n",
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "_agent_executor = create_react_agent(_llm, [weather, fish])\n",
    "rep = _agent_executor.invoke({\"messages\": [HumanMessage(\"艾溪江里有什么鱼\")]})\n",
    "rep[\"messages\"][-1].content"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
